package com.kern.util;

import com.kern.bean.DsConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created by caoshi at 9:56 2021/7/29
 */
public class JDBCUtil {

    /**
     * 加载驱动并获取连接
     * @param dsConn
     * @return
     * @throws ClassNotFoundException
     * @throws SQLException
     */
    public static Connection getConnection(DsConnection dsConn) throws ClassNotFoundException, SQLException {
        Class.forName(dsConn.getDriver());
        return DriverManager.getConnection(dsConn.getUrl(), dsConn.getUsername(), dsConn.getPassword());
    }

    /**
     * 关闭jdbc相关组件
     * @param connection
     * @param stat
     */
    public static void close(Connection connection, Statement stat) {
        try {
            if (connection != null) {
                connection.close();
            }
            if (stat != null) {
                stat.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }





}
